let JSArrayCompare = function (prop) {
    return function (obj1, obj2) {
        var val1 = obj1[prop];
        var val2 = obj2[prop];
        if (val1 < val2) {
            return -1;
        } else if (val1 > val2) {
            return 1;
        } else {
            return 0
        }
    }
};
//初始化获取url参数方法
let getUrlParam = function (name) {
    let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    let r = window.location.search.substr(1).match(reg);
    if (r != null)
        return unescape(r[2]);
    return null;
};
//获取指定URL的某项参数
let getUrlParam1 = function(url,name){
    let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    let urlParams = url.split('?');
    if(urlParams.length>1) {
        let r = urlParams[1].match(reg);
        if (r != null)
            return unescape(r[2]);
    }
    return null;
};
let get_string = function (str) {
    if (typeof (str) === "string") {
        return str;
    } else
        return "-";
};
let IsNotNullStr = function (str) {
    if (str == null || str == "" || str == "undefined")
        return false;
    else
        return true;
};
let MySubStr = function (str, length) {
    if (str.length > length) {
        return str.substring(0, length) + "...";
    } else {
        return str;
    }
};
let MyFirstWord = function (str) {
    let result = "";
    let re = new RegExp("[0-9a-zA-Z\_\.]");
    for (let i = 0; i < str.length; i++) {
        let r = str.substring(i, 1);
        if (re.test(r)) {
            result += r;
        } else {
            if (result != "") {
                return result;
            } else {
                return r;
            }
        }
    }
    return result;
};
let sub_xzqmc = function (name) {
    let returnname = name;
    if (name.length > 2) {
        if (name.indexOf("县") > 0 || name.indexOf("市") > 0) {
            returnname = name.substring(0, name.length - 1);
        }
        if (name.indexOf("自治") > 0) {
            returnname = name.substring(0, name.length - 3);
        }
    }
    return returnname;
};
let StrRemoveNull = function (str) {
    if (str == null || str == "" || str == "undefined" || typeof (str) == "undefined")
        return "";
    else
        return str;
};
let IsEndWithNumeric = function (str, len) {
    if (str.length < len) {
        return false;
    } else {
        let a = str.substr(str.length - len, len);
        if ($.isNumeric(a)) {
            return true;
        } else {
            return false;
        }
    }
};
let RemoveZF = function (str) {
    return str.replace(/[~'!<>@#$%^&*()-=+_\[\]']/g, "");
};
let isNullOrUndefined = function (obj) {
    if (typeof (obj) == "undefined" || obj == null) {
        return true;
    } else {
        return false;
    }
};
//度转度分秒
let CovertDegree = function (obj) {
    let num = parseFloat(obj);
    if ($.isNullOrUndefined(num))
        return num;
    else {
        let dg = parseInt(num);
        let fg = parseInt((num - dg) * 60);
        let sg = parseInt((num - dg) * 3600 - fg * 60);
        return dg.toString() + "°" + fg.toString() + "′" + sg.toString() + "″";
    }
};
//去除数组内空元素
let skipEmptyElementForArray = function (arr) {
    let a = [];
    $.each(arr, function (i, v) {
        let data = $.trim(v);//$.trim()函数来自jQuery
        if ('' != data) {
            a.push(data);
        }
    });
    return a;
};
//字符串加密
let compileStr = function (code) {
    let c = String.fromCharCode(code.charCodeAt(0) + code.length);
    for (let i = 1; i < code.length; i++) {
        c += String.fromCharCode(code.charCodeAt(i) + code.charCodeAt(i - 1));
    }
    return escape(c);
};
//字符串解密
let uncompileStr = function (code) {
    code = unescape(code);
    let c = String.fromCharCode(code.charCodeAt(0) - code.length);
    for (let i = 1; i < code.length; i++) {
        c += String.fromCharCode(code.charCodeAt(i) - c.charCodeAt(i - 1));
    }
    return c;
};
let removeJsonUndefinedParam = function (jsonObj) {
    for (let i in jsonObj) {
        let value = jsonObj[i];
        if (typeof (value) == "undefined") {
            delete jsonObj[i];
        }
    }
    return jsonObj;
};
/*
 * 验证url
 */
let checkUrl = function (url) {
    let strRegex = '^((https|http|ftp|rtsp|mms)?://)?'
        + '(([0-9a-z_!~*().&=+$%-]+: )?[0-9a-z_!~*().&=+$%-]+@)?'//ftp的user@
        + '(([0-9]{1,3}.){3}[0-9]{1,3}|'// IP形式的URL- 199.194.52.184
        + '([0-9a-z_!~*()-]+.)*'// 域名- www.
        + '[a-z]{2,6})'//域名的扩展名
        + '(:[0-9]{1,4})?'// 端口- :80
        + '((/?)|(/[0-9a-z_!~*().;?:@&=+$,%#-]+)+/?)$';
    return new RegExp(strRegex).test(url);
};
let UpperJsonKey = function (jsonObj) {
    for (let key in jsonObj) {
        jsonObj[key.toUpperCase()] = jsonObj[key];
        delete (jsonObj[key]);
    }
    return jsonObj;
};
let LowerJsonKey = function (jsonObj) {
    for (let key in jsonObj) {
        jsonObj[key.toLowerCase()] = jsonObj[key];
        delete (jsonObj[key]);
    }
    return jsonObj;
};
let toUtf8 = function (str) {
    let out, i, len, c;
    out = "";
    len = str.length;
    for (i = 0; i < len; i++) {
        c = str.charCodeAt(i);
        if ((c >= 0x0001) && (c <= 0x007F)) {
            out += str.charAt(i);
        } else if (c > 0x07FF) {
            out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
            out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));
            out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
        } else {
            out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F));
            out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
        }
    }
    return out;
};
let utf82Gb2312 = function (str) {
    let substr = "";
    let a = "";
    let b = "";
    let c = "";
    let i = "";
    i = str.indexOf("%");
    if (i == -1) {
        return str;
    }
    while (i != -1) {
        if (i < 3) {
            substr = substr += str.substr(0, i - 1);
            str = str.substr(i + 1, str.length - i);
            a = str.substr(0, 2);
            str = str.substr(2, str.length - 2);
            if (parseInt("0x" + a) & 0x80 == 0) {
                substr = substr + String.fromCharCode(parseInt("0x" + a));
            } else if (parseInt("0x" + a) & 0xE0 == 0xC0) {
                b = str.substr(1, 2);
                str = str.substr(3, str.length - 3);
                let widechar = (parseInt("0x" + a) & 0x1F) << 6;
                widechar = widechar | (parseInt("0x" + b) & 0x3F);
                substr = substr + String.fromCharCode(widechar);
            } else {
                b = str.substr(1, 2);
                str = str.substr(3, str.length - 3);
                c = str.substr(1, 2);
                str = str.substr(3, str.length - 3);
                let widechar = (parseInt("0x" + a) & 0x0F) << 12;
                widechar = widechar | ((parseInt("0x" + b) & 0x3F) << 6);
                widechar = widechar | (parseInt("0x" + c) & 0x3F);
                substr = substr + String.fromCharCode(widechar);
            }
        } else {
            substr = substr + str.substring(0, i);
            str = str.substring(i);
        }
        i = str.indexOf("%");
    }
    return substr + str;
};

let getTime = function () {
   let weeks = new Array("星期天","星期一","星期二","星期三","星期四","星期五","星期六");
   let date = new Date();
   let year = date.getFullYear();
   let month = date.getMonth()+1;
   let day = date.getDay();
   let week = date.getDay();
   let hour = date.getHours();
   let minutes = date.getMinutes();
   let seconds = date.getSeconds();
   return year+'年'+month+"月"+day+"日 "+weeks[week]+" "+hour+":"+minutes+":"+seconds;
};

let class2type = {};
let toString = class2type.toString;
let hasOwn = class2type.hasOwnProperty;
let isPlainObject = function (obj) {
    var proto, Ctor;
    if (!obj || this.toString.call(obj) !== "[object Object]") {
        return false;
    }
    proto = Object.getPrototypeOf(obj);
    if (!proto) {
        return true;
    }
    Ctor = this.hasOwn.call(proto, "constructor") && proto.constructor;
    return typeof Ctor === "function" && this.hasOwn.toString.call(Ctor) === this.hasOwn.toString.call(Object);
};
let extend = function () {
    // 默认不进行深拷贝
    var deep = false;
    var name, options, src, copy, clone, copyIsArray;
    var length = arguments.length;
    // 记录要复制的对象的下标
    var i = 1;
    // 第一个参数不传布尔值的情况下，target 默认是第一个参数
    var target = arguments[0] || {};
    // 如果第一个参数是布尔值，第二个参数是 target
    if (typeof target == 'boolean') {
        deep = target;
        target = arguments[i] || {};
        i++;
    }
    // 如果target不是对象，我们是无法进行复制的，所以设为 {}
    if (typeof target !== "object" && !isFunction(target)) {
        target = {};
    }
    // 循环遍历要复制的对象们
    for (; i < length; i++) {
        // 获取当前对象
        options = arguments[i];
        // 要求不能为空 避免 extend(a,,b) 这种情况
        if (options != null) {
            for (name in options) {
                // 目标属性值
                src = target[name];
                // 要复制的对象的属性值
                copy = options[name];
                // 解决循环引用
                if (target === copy) {
                    continue;
                }
                // 要递归的对象必须是 plainObject 或者数组
                if (deep && copy && (this.isPlainObject(copy) ||
                    (copyIsArray = Array.isArray(copy)))) {
                    // 要复制的对象属性值类型需要与目标属性值相同
                    if (copyIsArray) {
                        copyIsArray = false;
                        clone = src && Array.isArray(src) ? src : [];
                    } else {
                        clone = src && this.isPlainObject(src) ? src : {};
                    }
                    target[name] = extend(deep, clone, copy);
                } else if (copy !== undefined) {
                    target[name] = copy;
                }
            }
        }
    }
    return target;
};
/**
 * 格式化递归方法插入对象；
 * 若valueObject不存在pid，或者存在pid但不存在对应id的父节点，则直接添加至目标数组，
 * 若valueObject存在pid，存在对应id的父节点，则插入父节点的children数组中。
 * @param targetArray
 * @param valueObject
 * @param idKey
 * @param pidKey
 */
let formatInsertToArrayByPid = function(targetArray,valueObject,idKey,pidKey){
    let insert = formatInsertToArrayByPidSub(targetArray,valueObject,idKey,pidKey);
    if(!insert){
        targetArray.push(extend({},{children:[]},valueObject));
    }
};
/**
 * 格式化递归方法插入对象；
 * 若valueObject不存在pid，则直接添加至目标数组，
 * 若valueObject存在pid，存在对应id的父节点，则插入父节点的children数组中，
 * 若valueObject存在pid，但是不存在对应id的父节点，则不插入，
 * @param targetArray 目标数组
 * @param valueObject 待插入对象
 * @param idKey 节点id字段
 * @param pidKey 节点父级id字段
 * @returns {boolean} 是否插入成功
 */
let formatInsertToArrayByPidSub = function(targetArray,valueObject,idKey,pidKey){
    let insert = false;
    if(valueObject.hasOwnProperty(pidKey)){
        for(let i in targetArray){
            let item = targetArray[i];
            if(item[idKey]==valueObject[pidKey]){
                item.children.push(extend({},{children:[]},valueObject));
                insert = true;
                break;
            }else if(item.children.length>0){
                insert = formatInsertToArrayByPidSub(item.children,valueObject,idKey,pidKey);
            }
        }
    }else{
        targetArray.push(extend({},{children:[]},valueObject));
        insert = true;
    }
    return insert;
};
export {
    JSArrayCompare,
    getUrlParam,
    getUrlParam1,
    get_string,
    IsNotNullStr,
    MySubStr,
    MyFirstWord,
    sub_xzqmc,
    StrRemoveNull,
    IsEndWithNumeric,
    RemoveZF,
    isNullOrUndefined,
    CovertDegree,
    skipEmptyElementForArray,
    compileStr,
    uncompileStr,
    removeJsonUndefinedParam,
    checkUrl,
    UpperJsonKey,
    LowerJsonKey,
    toUtf8,
    utf82Gb2312,
    extend,
    getTime,
    formatInsertToArrayByPid,
    formatInsertToArrayByPidSub
}